package server

import (
	"fmt"
	"gitee.com/enjoy_5/goods-rpc/internal/model"
	"gitee.com/enjoy_5/goods-rpc/proto/goods"
	"strconv"
)

func goodsModelToPb(in *model.Goods) *goods.GoodsInfo {
	return &goods.GoodsInfo{
		Id:            int64(in.ID),
		Title:         in.Title,
		Point:         in.Point,
		Description:   in.Description,
		Img:           in.Img,
		PropertyInfos: nil,
	}
}
func GoodsSkuModelToPb(req *model.GoodsSku) *goods.SkuInfo {
	return &goods.SkuInfo{
		ProValueId1: strconv.FormatInt(req.ProValueId1, 10),
		ProValueId2: strconv.FormatInt(req.ProValueId2, 10),
		ProValueId3: strconv.FormatInt(req.ProValueId3, 10),
		Price:       float32(req.Price),
		VipPrice:    float32(req.VipPrice),
		Stock:       req.Stock,
		GoodsId:     req.GoodsId,
	}
}

//	func GoodsStockModelToPb(req *model.GoodsSku) *goods.GoodStockInfo {
//		return &goods.GoodStockInfo{
//			ID:  int64(req.ID),
//			Num: req.Stock,
//		}
//	}
func GetSkuInfoByID(skuId int64) (*goods.SkuInfo, error) {
	skuMod := new(model.GoodsSku)
	byID, err := skuMod.GetSkuInfoByID(skuId)
	if err != nil {
		return nil, err
	}

	pMod := new(model.GoodsPropertyValue)
	info1, err := pMod.GetGoodsPropertyValueByPId(byID.ProValueId1)
	if err != nil {
		return nil, err
	}
	info2, err := pMod.GetGoodsPropertyValueByPId(byID.ProValueId2)
	if err != nil {
		return nil, err
	}
	info3, err := pMod.GetGoodsPropertyValueByPId(byID.ProValueId3)
	if err != nil {
		return nil, err
	}
	return &goods.SkuInfo{
		ProValueId1: info1.Name,
		ProValueId2: info2.Name,
		ProValueId3: info3.Name,
		Price:       float32(byID.Price),
		VipPrice:    float32(byID.VipPrice),
		Stock:       byID.Stock,
		GoodsId:     byID.GoodsId,
	}, nil
}

func GetGoodsInfo(goodsId int64) (*goods.GoodsInfo, error) {
	goodsMod := new(model.Goods)
	goodsInfo, err := goodsMod.GetGoodsById(goodsId)
	if err != nil {
		return nil, err
	}

	propertyMod := new(model.GoodsProperty)
	propertyInfos, err := propertyMod.GetGoodsPropertyByGoodsId(goodsId)
	if err != nil {
		return nil, err
	}

	var propertyIds []int64
	for _, v := range propertyInfos {
		propertyIds = append(propertyIds, int64(v.ID))
	}

	propertyValueMod := new(model.GoodsPropertyValue)
	propertyValueInfos, err := propertyValueMod.GetGoodsPropertyValueByPropertyId(propertyIds)
	if err != nil {
		return nil, err
	}

	propertyValueMap := make(map[int64][]*goods.GoodsPropertyValueInfo)
	for _, v := range propertyValueInfos {
		propertyValueMap[v.PropertyId] = append(propertyValueMap[v.PropertyId], &goods.GoodsPropertyValueInfo{
			ID:                int64(v.ID),
			PropertyValueName: v.Name,
		})
	}

	res := goodsModelToPb(goodsInfo)
	for _, v := range propertyInfos {
		proValueInfos, ok := propertyValueMap[int64(v.ID)]
		if !ok {
			continue
		}
		res.PropertyInfos = append(res.PropertyInfos, &goods.GoodsPropertyInfo{
			ID:                 int64(v.ID),
			PropertyName:       v.Name,
			PropertyValueInfos: proValueInfos,
		})
	}
	return res, nil
}

func GetGoodsPriceByPropertyId(id1, id2, id3 int64) (*goods.SkuInfo, error) {
	skuMod := new(model.GoodsSku)
	infos, err := skuMod.GetGoodsSkuByPropertyValueIds(id1, id2, id3)
	if err != nil {
		return nil, err
	}
	return GoodsSkuModelToPb(infos), nil
}

func SearchGoodsByTitle(title string, page, pageSize int) ([]*goods.GoodsInfo, error) {
	mod := new(model.Goods)
	info, err := mod.GetGoodsListByPage(page, pageSize, title)
	if err != nil {
		return nil, err
	}
	var infos []*goods.GoodsInfo
	for _, v := range info {
		infos = append(infos, goodsModelToPb(v))
	}
	return infos, nil
}

// *
func GetGoodsInfoBySkuIds(skuId []int64) ([]*goods.SkuGoodsInfo, error) {
	skuMod := new(model.GoodsSku)
	//通过 skuid 查询数据
	byID, err := skuMod.GetGoodsInfoBySkuIds(skuId)
	if err != nil {
		return nil, err
	}
	//储存商品id
	var goodsIds []int64
	skuInfoMap := make(map[int64]*goods.SkuGoodsInfo)
	//第一次拼接
	for _, val := range byID {
		goodsIds = append(goodsIds, val.GoodsId)

		skuInfoMap[val.GoodsId] = &goods.SkuGoodsInfo{
			GoodsId:  val.GoodsId,
			Price:    float32(val.Price),
			VipPrice: float32(val.VipPrice),
			Stock:    val.Stock,
		}
	}

	goodMod := new(model.Goods)
	goodsList, err := goodMod.GetGoodsInfoByIds(goodsIds)
	if err != nil {
		return nil, err
	}
	//第二次拼接
	for _, val := range goodsList {
		if skuInfo, exists := skuInfoMap[int64(val.ID)]; exists {
			skuInfo.Title = val.Title
			skuInfo.Img = val.Img
		}
	}
	// 将 map 转换为 slice
	var skuInfo []*goods.SkuGoodsInfo
	for _, info := range skuInfoMap {
		skuInfo = append(skuInfo, info)
	}
	return skuInfo, nil
}

func GoodList(page, pageSize int, typeID int64, title string) ([]*goods.GoodListInfo, error) {
	mod := new(model.Goods)
	info, err := mod.GoodList(page, pageSize, typeID, title)
	if err != nil {
		return nil, fmt.Errorf("查询商品列表失败: %v", err) // 增加错误信息
	}

	var in []*goods.GoodListInfo

	for _, val := range info {
		in = append(in, &goods.GoodListInfo{
			ID:       val.ID,
			Title:    val.Title,
			Img:      val.Img,
			Price:    val.Price,
			VipPrice: val.VipPrice,
		})
	}

	return in, nil
}
